//
//  ShareFileManager.swift
//  Keyboard
//
//  Created by zhouzhanglin on 17/8/28.
//  Copyright © 2017年 周张林. All rights reserved.
//

import Foundation

/// 共享数据
class GroupShareData
{
    var backgroundColor:String? //背影颜色hex
    var backgroundImage:String? //背景图片
    var btnImage:String? //按钮图片
    var btnDefaultColor:String? //按钮默认颜色hex
    var btnHighlightColor:String? //按钮按下颜色hex
}


/// 共享数据管理类
class ShareFileManager {
    
    
    /// 共享group key
    static let groupIdentifier = "group.com.binghe.keyboard"
    
    static let groupUserDefault:UserDefaults? = UserDefaults(suiteName:groupIdentifier )

    
    /// 获取共享数据
    ///
    /// - Returns: 数据
    static func getGroupShareData()->GroupShareData?
    {
        if let userDef = groupUserDefault {
            let data = GroupShareData()
            data.backgroundColor = userDef.string(forKey: "backgroundColor")
            data.backgroundImage = userDef.string(forKey: "backgroundImage")
            data.btnImage = userDef.string(forKey: "btnImage")
            data.btnDefaultColor = userDef.string(forKey: "btnDefaultColor")
            data.btnHighlightColor = userDef.string(forKey: "btnHighlightColor")
            return data
        }
        return nil
    }
    
    
    /// 保存共享数据
    ///
    /// - Parameter data: 数据
    static func saveGroupShareData(data:GroupShareData!)->Void{
        if let userDef = groupUserDefault {
            userDef.set(data.backgroundColor, forKey: "backgroundColor")
            userDef.set(data.backgroundImage, forKey: "backgroundImage")
            userDef.set(data.btnImage, forKey: "btnImage")
            userDef.set(data.btnDefaultColor, forKey: "btnDefaultColor")
            userDef.set(data.btnHighlightColor, forKey: "btnHighlightColor")
            userDef.synchronize()
        }
    }
    
    
    /// 保存共享文件
    ///
    /// - Parameters:
    ///   - fileName: 文件名
    ///   - data: 数据
    static func saveGroupShareFile(fileName:String!,data:Data!)->Void{
        let fileManager = FileManager.default
        if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: groupIdentifier){
            fileManager.createFile(atPath: directory.appendingPathComponent(fileName).absoluteString, contents: data, attributes: nil)
            
            do {
                try data.write(to: directory.appendingPathComponent(fileName), options: NSData.WritingOptions.atomic)
            }
            catch {
                print("writing share file fail")
            }
            
        }
    }
    
    
    /// 获取共享文件
    ///
    /// - Parameter fileName: 文件名
    /// - Returns: 数据
    static func getGroupShareFile(fileName:String!)->Data?{
        let fileManager = FileManager.default
        if let directory = fileManager.containerURL(forSecurityApplicationGroupIdentifier: groupIdentifier){
            do {
                let data = try Data.init(contentsOf: directory.appendingPathComponent(fileName))
                return data
            }
            catch {
                print("read shared fail")
            }
        }
        return nil
    }
}
